package com.cloudwebrtc.voip.sipenginev2.impl;

import com.cloudwebrtc.voip.mediaengine.MediaStream;
import com.cloudwebrtc.voip.mediaengine.impl.MediaStreamImpl;
import com.cloudwebrtc.voip.sipenginev2.Call;
import com.cloudwebrtc.voip.sipenginev2.CallReport;
import com.cloudwebrtc.voip.sipenginev2.CallState;
import com.cloudwebrtc.voip.sipenginev2.Direction;
import com.cloudwebrtc.voip.sipenginev2.DtmfMethod;
import com.cloudwebrtc.voip.sipenginev2.SipProfile;

public class CallImpl implements Call {
	public CallImpl(long aNativePtr)  {
		nativePtr = aNativePtr;
	}
	//implement for Call_JNI.cpp
	protected final long nativePtr;
	private native void Accept(long nativePtr);
	private native void Accept2(long nativePtr,boolean send_audio, boolean send_video);
	private native void Reject(long nativePtr,int code, String reason);
	private native int Hangup(long nativePtr);
	private native int UpdateCall(long nativePtr,boolean enable_video);
	private native int Hold(long nativePtr);
	private native int UnHold(long nativePtr);
	private native int SendDtmf(long nativePtr,int dtmf_type, String tone,boolean play_dtmf_tone);
	private native String GetCallerId(long nativePtr);
	private native int GetDirection(long nativePtr);
	private native int GetCallState(long nativePtr);
	private native boolean GetSupportVideo(long nativePtr);
	private native boolean GetSupportData(long nativePtr);
	private native long GetProfile(long nativePtr);
	private native long GetMediaStream(long nativePtr);
	private native int GetErrorCode(long nativePtr);
	private native String GetErrorReason(long nativePtr);
	private native long GetCallReport(long nativePtr);
	private native String CallStateName(long nativePtr,int state);
	private native String GetUniqueId(long nativePtr);
	private native String GetDeviceType(long nativePtr);
	
	@Override
	public void Accept() {
		Accept(nativePtr);
	}
	@Override
	public void Reject(int code, String reason) {
		Reject(nativePtr,code,reason);
	}
	@Override
	public int Hangup() {
		return Hangup(nativePtr);
	}
	@Override
	public int UpdateCall(boolean enable_video) {
		return UpdateCall(nativePtr,enable_video);
	}
	@Override
	public int Hold() {
		return Hold(nativePtr);
	}
	@Override
	public int UnHold() {
		return UnHold(nativePtr);
	}
	@Override
	public int SendDtmf(DtmfMethod dtmf_method, String tone,
			boolean play_dtmf_tone) {
		return SendDtmf(nativePtr,dtmf_method.IntgerValue(),tone,play_dtmf_tone);
	}
	@Override
	public String GetCallerId() {
		return GetCallerId(nativePtr);
	}
	@Override
	public Direction GetDirection() {
		int dir_int = GetDirection(nativePtr);
		return (dir_int == Direction.Incoming.IntgerValue())
				? Direction.Incoming : Direction.Outgoing;
	}
	@Override
	public CallState GetCallState() {
		int state_int = GetCallState(nativePtr);
		
		if(state_int == CallState.NewCall.IntgerValue())
			return CallState.NewCall;
		else if(state_int == CallState.Cancel.IntgerValue())
			return CallState.Cancel;
		else if(state_int == CallState.Failed.IntgerValue())
			return CallState.Failed;
		else if(state_int == CallState.Rejected.IntgerValue())
			return CallState.Rejected;
		else if(state_int == CallState.EarlyMedia.IntgerValue())
			return CallState.EarlyMedia;
		else if(state_int == CallState.Ringing.IntgerValue())
			return CallState.Ringing;
		else if(state_int == CallState.Answered.IntgerValue())
			return CallState.Answered;
		else if(state_int == CallState.Hangup.IntgerValue())
			return CallState.Hangup;
		else if(state_int == CallState.Pausing.IntgerValue())
			return CallState.Pausing;
		else if(state_int == CallState.Paused.IntgerValue())
			return CallState.Paused;
		else if(state_int == CallState.Resuming.IntgerValue())
			return CallState.Resuming;
		else if(state_int == CallState.Resumed.IntgerValue())
			return CallState.Resumed;
		else if(state_int == CallState.Updating.IntgerValue())
			return CallState.Updating;
		else if(state_int == CallState.Updated.IntgerValue())
			return CallState.Updated;
		
		return CallState.Unknown;
	}
	@Override
	public boolean GetSupportVideo() {
		return GetSupportVideo(nativePtr);
	}
	@Override
	public boolean GetSupportData() {
		return GetSupportVideo(nativePtr);
	}
	@Override
	public SipProfile GetProfile() {
		long sipProfilePtr = GetProfile(nativePtr);
		return new SipProfileImpl(sipProfilePtr);
	}
	@Override
	public MediaStream GetMediaStream() {
		long mediaStreamPtr = GetMediaStream(nativePtr);
		return new MediaStreamImpl(mediaStreamPtr);
	}
	@Override
	public int GetErrorCode() {
		return GetErrorCode(nativePtr);
	}
	@Override
	public String GetErrorReason() {
		return GetErrorReason(nativePtr);
	}
	@Override
	public CallReport GetCallReport() {
		long callReportPtr = GetCallReport(nativePtr);
		return new CallReportImpl(callReportPtr);
	}
	@Override
	public String CallStateName(CallState state) {
		return CallStateName(nativePtr, state.IntgerValue());
	}
	@Override
	public String GetUniqueId() {
		return GetUniqueId(nativePtr);
	}
	@Override
	public void Accept(boolean send_audio, boolean send_video) {
		Accept2(nativePtr,send_audio,send_video);
	}
	@Override
	public String GetDeviceType()
	{
	    return GetDeviceType(nativePtr);
	}
}
